<?php

/**
 * @author Rizky Tahara Shita
 * @website http://tahara.web.id
 * @email rizky.tahara@gmail.com
 * @copyright b'IT Cyber Solution
 */

class M_content extends BZ_Model
{
/**
 * @section BISKITZ_FRONT Content Model
 * @description the model for biskitz front-end content
 */

	/**
	 * @function details
	 * @description the content detail
	 */
	function details
		(
			$prm_dbase = 'db_default'
			, $prm_type = 'Post'
			, $prm_table_where = '', $prm_content_link_use = TRUE
			, $prm_record_limit = 20
			, $prm_fields_order = ''
			, $prm_table_or_like = ''
			, $prm_comments_width = 60
			, $prm_excerpt_use = FALSE
			, $prm_title_link_use = 'TRUE'
		)
	{

		$fields = 'id, type, title, slug'
			. ', date_format(date, "'.$this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_DATE_TIME_FORMAT'].'") as date, date_format(date, "%Y") as year, date_format(date, "%m") as month, date_format(date, "%d") as day'
			. ', content, biskitz_user__id'
			. ', (select name from bz_biskitz_user c where c.id = a.biskitz_user__id) as user_name';

		switch ($prm_type)
		{
			case 'Post':
				$table = array
					(
						'NAME' => 'biskitz_front_content a'
						, 'FIELDS' => $fields
								. ', (select name from bz_biskitz_front_category_lang b where a.front_category__id = b.front_category__id and language__id = "'.$this->web_data['BISKITZ_FRONT']['LANGUAGE'][0]['id'].'") as filled_under'
								. ', comments, url'
					);
				break;


			case 'Page':
				$table = array
					(
						'NAME' => 'biskitz_front_menu a'
						, 'FIELDS' => $fields
					);
				break;
		}


		$content_data =	$this->M_database->datas
			(
				$prm_dbase
				, $table['NAME']
				, array_merge
					(
						array('language__id' => $this->web_data['BISKITZ_FRONT']['LANGUAGE'][0]['id'])
						, $prm_table_where
					)
				, $table['FIELDS']
				, FALSE
				, $prm_fields_order
				, $prm_table_or_like
				, $prm_record_limit, -1
				, FALSE
				, FALSE
			);

		if (is_array($content_data))
		{
			/**
			 * @description load the helpers
			 */
			$this->load->helper(array('text', 'smiley'));
			/**
			 * @description initialize the smiley variables
			 */
			$smiley = array
				(
					'PATH' => site_url('themes/'.$this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_THEME'].'/images/smileys')
				);

			/**
			 * @description initialize the comments variables
			 */
			$comments_data = array
				(
					'TITLE' => $this->lang->line('comments_list')
					, 'DATA_SHOW' => ''
					, 'DATA' => ''
					, 'FORM' => ''
				);


			foreach ($content_data as $rows)
			{
				if ($rows['type'] == 'Post' && $rows['comments'] == 'TRUE' && $prm_content_link_use == FALSE)
				{
					/**
					 * @description use comments ?
					 */
					$comments_data = $this->comments_list
						(
							$prm_dbase
							, $rows['id']
							, $prm_comments_width
						);

					$comments_data = array
						(
							'TITLE' => $this->lang->line('comments_list')
							, 'DATA_SHOW' => is_array($comments_data) ? 'TRUE' : $this->lang->line('comments_list_no_record')
							, 'DATA' => $comments_data
							, 'FORM' => array
								(
									'TITLE' => $this->lang->line('comments_form')
									, 'JAVASCRIPT' => '
											<script>
												$(function()
												{
													$("#btn_comment_submit").click
														(function()
														{
															js_form_action_validate
															(
																"'.site_url(array('read')).'", "ajx_comment_send", "", ""
																, "#frm_biskitz_front_comment", "#process", "#error_message"
																, "validate_comment"
															);
														});
												});
											</script>'
									, 'OPEN' => form_open('', array('id' => 'frm_biskitz_front_comment', 'onSubmit' => 'return false'), '')
									, 'CONTENT' => array
										(
											'ID' => $rows['id']
											, 'NAME' => $this->lang->line('comments_form_name')
											, 'EMAIL' => $this->lang->line('comments_form_email')
											, 'WEBSITE' => $this->lang->line('comments_form_website')
											, 'COMMENT' => $this->lang->line('comments_form_comment')
											, 'SUBMIT' => $this->lang->line('comments_form_button_submit')
										)
								)
						);
				}


				/**
				 * @description generate the content detail
				 */
				$post_data[] = array
					(
						/**
						 * @description content detail
						 */
						'id' => $rows['id']
						, 'link' => $rows['type'] == 'Post'
								? site_url('read/posts/'.$rows['year'].'/'.$rows['month'].'/'.$rows['day'].'/'.$rows['slug'])
								: site_url('read/pages/'.$rows['slug'])
						, 'title' => $rows['title']
						, 'title_link_use' => $prm_title_link_use
						, 'title_tooltips' => sprintf($this->lang->line('post_title_tooltips'), $rows['title'])
						, 'date' => $rows['date']
						, 'content' => $prm_excerpt_use == 'TRUE'
								? word_limiter
									(
										parse_smileys( strip_tags($rows['content']), $smiley['PATH'])
										, $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_POST_EXCERPT_LIMIT']
									)
								: parse_smileys($rows['content'], $smiley['PATH'])
						, 'url' => isset($rows['url']) ? $rows['url'] : ''
						, 'type' => $rows['type']
						, 'user' => $rows['biskitz_user__id']
						, 'posted_by' => $rows['user_name']
						, 'filled_under' => $prm_type == 'Post' ? $rows['filled_under'] : ''
						, 'comment_use' => $prm_type == 'Post' ? $rows['comments'] : 'FALSE'
					);
			}


			$return_value = array
				(
					'POST_DATA' => $post_data
					, 'POST_LINK' => array
						(
							'use' => $prm_content_link_use
							, 'detail_title' => $this->lang->line('post_link_detail')
							, 'comments_read_title' => $this->lang->line('post_link_comments_read')
							, 'comments_write_title' => $this->lang->line('post_link_comments_write')
						)
					, 'POST_COMMENT' => $comments_data
				);
		}
		else
		{
			$return_value[] = array
				(
					'Error'
					, $this->lang->line('msg_post_no_data')
				);
		}


		return $return_value;
	}



/**
 * @section COMMENTS
 */
	/**
	 * @function comments_list
	 * @description generate the comment list
	 */
	function comments_list($prm_dbase = 'db_default', $prm_content_id = '', $prm_comments_width = 60)
	{
		$comments_data =	$this->M_database->datas
			(
				$prm_dbase
				, 'biskitz_front_comment'
				, trim($prm_content_id) == ''
						? array('approve' => 'Approved')
						: array('front_content__id' => $prm_content_id, 'approve' => 'Approved')
				, 'name, date, website, email, content'
				, FALSE
				, 'id asc'
				, ''
				, '', -1
				, FALSE
				, FALSE
			);

		if (is_array($comments_data))
		{
			/**
			 * @description load the helpers
			 */
			$this->load->helper(array('text', 'smiley'));
			/**
			 * @description initialize the smiley variables
			 */
			$smiley = array
				(
					'PATH' => site_url
						(
							array
							(
								'themes'
								, $this->web_data['BISKITZ_FRONT']['SETTINGS']['BISKITZ_FRONT_THEME']
								, 'images'
								, 'smileys'
							)
						)
				);

			foreach ($comments_data as $comments_rows)
			{
				$return_value[] = array
					(
						'name' => htmlentities(word_wrap($comments_rows['name'], $prm_comments_width), ENT_QUOTES)
						, 'date' => $this->lang->line('comments_list_commented_at').$comments_rows['date']
						, 'website' => trim($comments_rows['website']) == '' ? '' : htmlentities(word_wrap($comments_rows['website'], $prm_comments_width), ENT_QUOTES)
						, 'email' => htmlentities(word_wrap($comments_rows['email'], $prm_comments_width), ENT_QUOTES)
						, 'content' => parse_smileys(nl2br(htmlentities(word_wrap($comments_rows['content'], $prm_comments_width), ENT_QUOTES)), $smiley['PATH'])
					);
			}
		}
		else
		{
			$return_value = '';
		}


		return $return_value;
	}



}

/** End of file m_content.php */
/** Location: ./biskitz_mpc/biskitz/models/biskitz/m_content.php */